package rpc
{
	import events.HTTPCommanderEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.utils.ObjectUtil;

	public class HTTPCommanderDelegate extends EventDispatcher implements IHTTPCommanderDelegate
	{
		private var _target:Object;
		private var _dataProvider:Object;
		private var _resultData:Object;
		
		public function HTTPCommanderDelegate(target:IEventDispatcher=null)
		{
			super(target);
			addEventListener(HTTPCommanderEvent.HTTPCOMMANDER_DELEGATE,delegateHandler,false,0,true);
			
		}
		
		public function delegateHandler(event:HTTPCommanderEvent):void
		{
			if(event.currentTarget is IHTTPCommanderDelegate){
				_resultData = _resultData ? _resultData : _dataProvider;
				_target.dispatchEvent(new HTTPCommanderEvent(HTTPCommanderEvent.HTTPCOMMANDER_DELEGATE_COMPLETE,ObjectUtil.copy(_resultData),false,true));
				_resultData = null;
				_dataProvider = null;
			}
		}
		
		public function get target():Object
		{
			return _target;
		}
		
		public function set target(value:Object):void
		{
			_target = value;
		}
		
		public function get dataProvider():Object
		{
			return _dataProvider;
		}
		
		public function set dataProvider(value:Object):void
		{
			_dataProvider = value;
		}
		
		public function get resultData():Object
		{
			return _resultData;
		}
		
		public function set resultData(value:Object):void
		{
			_resultData = value;
		}
		
	}
}